﻿///Code based on
/// http://chessbin.com/page/Chess-Game-Starer-Kit.aspx

using System.Collections.Generic;
using Core.Models.Enums;

namespace Core.Models
{
    internal sealed class Piece
    {
        #region vars

        internal ChessPieceColor PieceColor;
        internal ChessPieceType PieceType;
        internal short PieceValue;
        internal short PieceActionValue;

        internal short IsAttackedBy;
        internal short IsDefendedBy;
        internal short IsAttacking;
        internal short IsDefending;
        internal short nbIsDefendedBy;
        internal short nbIsAttackedBy;
        internal bool Selected;
        internal bool Moved;
        internal Stack<byte> ValidMoves;
        internal int LastValidMoveCount;

        #endregion

        #region constructor
        internal Piece(Piece piece)
        {
            PieceColor = piece.PieceColor;
            PieceType = piece.PieceType;
            Moved = piece.Moved;
            PieceValue = piece.PieceValue;

            if (piece.ValidMoves != null)
                LastValidMoveCount = piece.ValidMoves.Count;
        }

        internal Piece(ChessPieceType chessPiece, ChessPieceColor chessPieceColor)
        {
            PieceType = chessPiece;
            PieceColor = chessPieceColor;
            ValidMoves = new Stack<byte>();

            PieceValue = CalculatePieceValue(PieceType);
            PieceActionValue = CalculatePieceActionValue(PieceType);
        }

        #endregion

        #region methods
        /// <summary>
        /// Used in the constructor to calculate the value of the piece
        /// </summary>
        /// <param name="pieceType"></param>
        /// <returns></returns>
        private static short CalculatePieceValue(ChessPieceType pieceType)
        {
            switch (pieceType)
            {
                case ChessPieceType.Pawn:
                    {
                        return 1;

                    }
                case ChessPieceType.Knight:
                    {
                        return 3;
                    }
                case ChessPieceType.Bishop:
                    {
                        return 3;
                    }
                case ChessPieceType.Rook:
                    {
                        return 5;
                    }
                case ChessPieceType.Queen:
                    {
                        return 9;
                    }

                case ChessPieceType.King:
                    {
                        return 200;
                    }
                default:
                    {
                        return 0;
                    }
            }
        }

        /// <summary>
        /// Calculate the value of the piece for the score
        /// </summary>
        /// <param name="pieceType"></param>
        /// <returns></returns>
        private static short CalculatePieceActionValue(ChessPieceType pieceType)
        {
            switch (pieceType)
            {
                case ChessPieceType.Pawn:
                    {
                        return 6;
                    }
                case ChessPieceType.Knight:
                    {
                        return 3;
                    }
                case ChessPieceType.Bishop:
                    {
                        return 3;
                    }
                case ChessPieceType.Rook:
                    {
                        return 2;
                    }
                case ChessPieceType.Queen:
                    {
                        return 1;
                    }
                case ChessPieceType.King:
                    {
                        return 1;
                    }
                default:
                    {
                        return 0;
                    }
            }
        }

        internal static string GetPieceTypeShort(ChessPieceType pieceType)
        {
            switch (pieceType)
            {
                case ChessPieceType.Pawn:
                    {
                        return "P";
                    }
                case ChessPieceType.Knight:
                    {
                        return "N";
                    }
                case ChessPieceType.Bishop:
                    {
                        return "B";
                    }
                case ChessPieceType.Rook:
                    {
                        return "R";
                    }

                case ChessPieceType.Queen:
                    {
                        return "Q";
                    }

                case ChessPieceType.King:
                    {
                        return "K";
                    }
                default:
                    {
                        return "P";
                    }
            }
        }
        #endregion
    }
}
